--- --- hook  掉 inst.AnimState  不依赖mod加载优先级，方便和其他MOD兼容


modimport("Original_AnimState_Upgrade_for_npc/Special_hook_TheSim.lua")    --- 更新 PlayAnimation 和 PushAnimation 



TUNING.NPC_UPGRADE_ANIMSTATE_FN = {}
if TUNING.NPC_MOD_TEST_DEBUG == true then
    -- modimport("Original_AnimState_Upgrade_for_npc/00_PlayAnim_PushAnim.lua")    --- 更新 PlayAnimation 和 PushAnimation 
    -- modimport("Original_AnimState_Upgrade_for_npc/01_GetBuild.lua")    --- 更新 PlayAnimation 和 PushAnimation 
end

local function theAnimState_fn_Upgrade(theAnimState)
    for fn_name, fn in pairs(TUNING.NPC_UPGRADE_ANIMSTATE_FN) do
        if fn and type(fn) == "function" then
            fn(theAnimState)
        end
    end
end

local function hook_player_animstate(inst)      ----- DoTaskInTime 0 时候使用， 数据进入 形式不一样  
    local tempInst = inst
    -----------------------------------------------------------------------------------------------------------------------------
    ------ 添加关键储存API
    ------ inst.AnimState:save_inst_4_npc(inst.AnimState,inst)
    ------ inst.AnimState:get_inst_4_npc(inst.AnimState)
    ------ 在theAnimState的func里，用 self.get_inst_4_npc(self,self) 得到inst


    --------- 避免重复hook,其他角色进入则参数进入即可
    if tempInst.AnimState.save_inst_4_npc then
        if type(tempInst.AnimState) == "userdata" then
            inst.AnimState:save_inst_4_npc(inst.AnimState,inst) --- 注入方式不一样
        elseif type(tempInst.AnimState) == "table" then
            inst.AnimState:save_inst_4_npc(inst)    ----- 注入方式不一样                
        end
        print("info npc mod: already hook animstate ,data join and func return")
        return
    end



    if type(tempInst.AnimState) =="userdata" then
    --- 部分mod 会 hook 掉 inst.AnimState ，例如 棱镜，把userdata 转为 table，在这里 分开处理这部分
        print("npc mod hook player AnimState   userdata")
        local theAnimState = getmetatable(tempInst.AnimState).__index
        theAnimState.save_inst_4_npc = function(self,inst_animstate,inst)
            if getmetatable(self).__index.save_inst_data_4_npc == nil then
                getmetatable(self).__index.save_inst_data_4_npc = {}
            end
            getmetatable(self).__index.save_inst_data_4_npc[inst_animstate] = inst
        end

        theAnimState.get_inst_4_npc = function(self,the_animstate)
            -- self is userdata
            if getmetatable(self).__index.save_inst_data_4_npc then
                return getmetatable(self).__index.save_inst_data_4_npc[the_animstate]
            end
            return nil
        end        

        inst.AnimState:save_inst_4_npc(inst.AnimState,inst) --- 注入方式不一样
        theAnimState_fn_Upgrade(theAnimState)

    elseif type(tempInst.AnimState) == "table" then
    --- 部分mod 会 hook 掉 inst.AnimState ，例如 棱镜，把userdata 转为 table，在这里 分开处理这部分
        print("npc mod hook player AnimState   table")
        local theAnimState = tempInst.AnimState
        function theAnimState:save_inst_4_npc(inst)
            if self.save_inst_data_4_npc == nil then
                self.save_inst_data_4_npc = {}
            end
            self.save_inst_data_4_npc[self] = inst
        end

        function theAnimState:get_inst_4_npc(the_animstate)
            if self.save_inst_data_4_npc then
                return self.save_inst_data_4_npc[the_animstate]
            end
            return nil
        end

        inst.AnimState:save_inst_4_npc(inst)    ----- 注入方式不一样
        theAnimState_fn_Upgrade(theAnimState)

    end

    -----------------------------------------------------------------------------------------------------------------------------
end

AddPlayerPostInit(function(inst)
    if not TheWorld.ismastersim then
		return
	end

    inst:DoTaskInTime(0,function()  --- 玩家完全进入游戏后。再侵入。不然遭遇棱镜会失效        
        hook_player_animstate(inst)
    end)
end)